﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RoomControl
{
    public partial class RoomControl: UserControl
    {
        private string roomName;
        public Color defaultColor = Color.Gray;
        public Color highlightColor = Color.Blue;

        public event EventHandler<MovementClickEventArgs> MovementClick;


        public RoomControl()
        {
            InitializeComponent();


        }

        private void UserControl1_Resize(object sender, EventArgs e)
        {

        }

        public String RoomName
        {
            get
            {
                return roomName;
            }
            set
            {
                roomName = value;
                txtRoomName.Text = value;
            }
        }

        public void AddPlayer(Color c)
        {
            if (picPlayer1.BackColor == Color.Tan)
            {
                picPlayer1.BackColor = c;
            }
            else if (picPlayer2.BackColor == Color.Tan)
            {
                picPlayer2.BackColor = c;
            }
            else if (picPlayer3.BackColor == Color.Tan)
            {
                picPlayer3.BackColor = c;
            }
            else if (picPlayer4.BackColor == Color.Tan)
            {
                picPlayer4.BackColor = c;
            }
            else if (picPlayer5.BackColor == Color.Tan)
            {
                picPlayer5.BackColor = c;
            }
            else if (picPlayer6.BackColor == Color.Tan)
            {
                picPlayer6.BackColor = c;
            }
            else
            {
                throw new Exception("No spaces left in RoomControl " + this.Name + " to add another player.");
            }
        }

        public void RemovePlayer()
        {
            picPlayer1.BackColor = Color.Tan;
            picPlayer2.BackColor = Color.Tan;
            picPlayer3.BackColor = Color.Tan;
            picPlayer4.BackColor = Color.Tan;
            picPlayer5.BackColor = Color.Tan;
            picPlayer6.BackColor = Color.Tan;
        }

        public void RemovePlayer(Color c)
        {
            if (picPlayer1.BackColor == c)
            {
                picPlayer1.BackColor = Color.Tan;
            }
            else if (picPlayer2.BackColor == c)
            {
                picPlayer2.BackColor = Color.Tan;
            }
            else if (picPlayer3.BackColor == c)
            {
                picPlayer3.BackColor = Color.Tan;
            }
            else if (picPlayer4.BackColor == c)
            {
                picPlayer4.BackColor = Color.Tan;
            }
            else if (picPlayer5.BackColor == c)
            {
                picPlayer5.BackColor = Color.Tan;
            }
            else if (picPlayer6.BackColor == c)
            {
                picPlayer6.BackColor = Color.Tan;
            }
            else
            {
                throw new Exception("Color in RoomControl " + this.Name + " was not found.");
            }
        }

        public void SetHighlight()
        {
            this.BackColor = highlightColor;
        }

        public void RemoveHighlight()
        {
            this.BackColor = defaultColor;
        }

        private void txtRoomName_TextChanged(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void picPlayer3_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void picPlayer2_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void picPlayer1_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void picPlayer4_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void picPlayer5_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void picPlayer6_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void RoomControl_Load(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        private void RoomControl_Click(object sender, EventArgs e)
        {
            SendMovementClickMessage(RoomName);
        }

        /// <summary>
        /// Start Trun
        /// </summary>
        /// <param name="s"></param>
        private void SendMovementClickMessage(String s)
        {
            MovementClickEventArgs args = new MovementClickEventArgs();
            args.room = s;
            OnMovementClickMessage(args);
        }
        protected virtual void OnMovementClickMessage(MovementClickEventArgs e)
        {
            EventHandler<MovementClickEventArgs> handler = MovementClick;
            if (handler != null)
            {
                handler(this, e);
            }
        }


    }

    public class MovementClickEventArgs : EventArgs
    {
        public String room { get; set; }
    }
}
